//
//  GMTrainViewCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/6/27.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMTrainViewCell.h"
#import "XZTrainRecord.h"

@interface GMTrainViewCell ()
//培训名称
@property (weak, nonatomic) IBOutlet UILabel *TrainName;
//状态
@property (weak, nonatomic) IBOutlet UILabel *StatusName;
//类别
@property (weak, nonatomic) IBOutlet UILabel *TypeName;
//级别
@property (weak, nonatomic) IBOutlet UILabel *GradeName;
//起止时间
@property (weak, nonatomic) IBOutlet UILabel *StartEndTimeName;
//理论
@property (weak, nonatomic) IBOutlet UILabel *Theory;
//实操
@property (weak, nonatomic) IBOutlet UILabel *Oper;

@end
//以结业
#define KPASSColor [UIColor colorWithHexString:@"#009900" andAlpha:1.0]
//未结业
#define KUNPASSColor [UIColor colorWithHexString:@"#FF0000" andAlpha:1.0]
//培训中
#define KTrainColor [UIColor colorWithHexString:@"#FF9900" andAlpha:1.0]
@implementation GMTrainViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (void)setModel:(XZTrainRecord *)model{
    _model = model;
    //培训名称
    self.TrainName.text = model.TrainName;
    //状态
    if ([model.StatusName isEqualToString:@"待审核"] || [model.StatusName isEqualToString:@"未结业"]) {
        self.StatusName.textColor = KUNPASSColor;
    }else if ([model.StatusName isEqualToString:@"培训中"]){
        self.StatusName.textColor = KTrainColor;
    }else if ([model.StatusName isEqualToString:@"已结业"]){
        self.StatusName.textColor = KPASSColor;
    }
    self.StatusName.text = model.StatusName;
    //类别
    self.TypeName.text = [NSString stringWithFormat:@"类别：%@",model.TypeName];
    //级别
    self.GradeName.text = [NSString stringWithFormat:@"级别：%@",model.GradeName];
    //起止时间
    self.StartEndTimeName.text = model.StartEndTimeName;
    
    //理论
    if ([model.TheoryStatus isEqualToNumber:@(0)]) {//未考试
        self.Theory.text = @"理论：未考试";
    }else if ([model.TheoryStatus isEqualToNumber:@(1)]){//通过
        NSString *passText = [NSString stringWithFormat:@"理论：%@分(通过)",model.TheoryScore];
        NSString *scoreString = [NSString stringWithFormat:@"%@",model.TheoryScore];
        [self textAttributedString:passText scoreString:scoreString label:self.Theory];
    }else if ([model.TheoryStatus isEqualToNumber:@(2)]){//未通过
        NSString *unPassText = [NSString stringWithFormat:@"理论：%@分(未通过)",model.TheoryScore];
        NSString *unScoreString = [NSString stringWithFormat:@"%@",model.TheoryScore];
        [self textAttributedString:unPassText scoreString:unScoreString label:self.Theory];
    }
    
    //实操
    if ([model.OperStatus isEqualToNumber:@(-1)]) {//不要求
        self.Oper.text = @"实操：不要求";
        self.Oper.textColor = kDarkGrayColor;
    }else if ([model.OperStatus isEqualToNumber:@(0)]){//未测试
        self.Oper.text = @"实操：未测试";
        self.Oper.textColor = kDarkGrayColor;
    }else if ([model.OperStatus isEqualToNumber:@(1)]){//通过
        NSString *passText = [NSString stringWithFormat:@"实操：%@分(通过)",model.OperScore];
        NSString *scoreString = [NSString stringWithFormat:@"%@",model.OperScore];
        [self textAttributedString:passText scoreString:scoreString label:self.Oper];
    }else if ([model.OperStatus isEqualToNumber:@(2)]){//未通过
        NSString *unPassText = [NSString stringWithFormat:@"实操：%@分(未通过)",model.OperScore];
        NSString *unScoreString = [NSString stringWithFormat:@"%@",model.OperScore];
        [self textAttributedString:unPassText scoreString:unScoreString label:self.Oper];
    }
}

- (void)textAttributedString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label{
    
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(3, scoreString.length)];
    #define KTrainColor [UIColor colorWithHexString:@"#FF9900" andAlpha:1.0]
    //字符串末尾有(未通过)
    if ([string hasSuffix:@"(未通过)"]) {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KUNPASSColor range:NSMakeRange(string.length-5, 5)];
        
    } else {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KPASSColor range:NSMakeRange(string.length-4, 4)];
    }
    label.attributedText = originalAttributedString;
}

@end
